#include <QHBoxLayout>
#include <QCheckBox>
#include <QColorDialog>
#include <QPushButton>
#include <QButtonGroup>
#include <QRadioButton>
#include <QFontDialog>
#include <QSpinBox>
#include <QLabel>
#include <QDebug>
#include <QFont>
#include <QPixmap>
#include <QColor>
#include <QPoint>

#include "stocksetting.h"

StockSetting::StockSetting(QWidget *parent)
    : QWidget(parent)
{
    resize(400, 300);
    initWindow();
}

void StockSetting::initMicroDisplaySetting(int flag, QFont font, QColor color,
                                           QPoint point)
{
    m_pBTGroup->button(flag)->setChecked(true);

    m_pFontBT->setFont(font);

    QString str = QString::fromLatin1("background-color:%1").arg(color.name());
    m_pColorBT->setStyleSheet(str);

    m_pSpinBoxX->setValue(point.x());
    m_pSpinBoxY->setValue(point.y());
}

void StockSetting::indexWidget()
{
    m_pListWidget = new QListWidget(this);
    m_pListWidget->setMinimumWidth(80);
    m_pListWidget->setMaximumWidth(120);
    m_pListWidget->setMinimumHeight(270);

    QStringList strlist = QStringList()<<tr("迷你显示")<<"item2"<<"item3";
    m_pListWidget->addItems(strlist);

    connect(m_pListWidget, SIGNAL(currentRowChanged(int)), this,
            SLOT(slotRowChanged(int)));
}

QWidget* StockSetting::createTabOneInFPW()
{
    QWidget *widget = new QWidget;

    //是否显示
    m_pBTGroup = new QButtonGroup(widget);

    QRadioButton *alwaysRB = new QRadioButton(tr("总是显示"), widget);
    QRadioButton *hideRB = new QRadioButton(tr("最小化时显示"), widget);
    QRadioButton *neverRB = new QRadioButton(tr("不显示"), widget);

    hideRB->setChecked(true);

    m_pBTGroup->addButton(alwaysRB, MIC_ALWAYS_DIS);
    m_pBTGroup->addButton(hideRB, MIC_HIDE_DIS);
    m_pBTGroup->addButton(neverRB, MIC_NERVER_DIS);
    connect(m_pBTGroup, SIGNAL(buttonClicked (int)), this, SLOT(slotMicroDisplay(int)));

    //字体和颜色
    m_pDisplayGB = new QGroupBox(widget);
    m_pDisplayGB->setGeometry(10,50,230,85);
    m_pDisplayGB->setTitle(tr("字体和颜色"));

    QLabel *fontLabel = new QLabel(tr("字体:"), m_pDisplayGB);
    fontLabel->setGeometry(10,32,45,15);

    QLabel *colorLabel = new QLabel(tr("颜色:"), m_pDisplayGB);
    colorLabel->setGeometry(10,72,45,15);

    m_pFontBT = new QPushButton(m_pDisplayGB);
    m_pFontBT->setGeometry(55,25,45,30);
    m_pFontBT->setFont(QFont("Arial", 12, -1, false));
    m_pFontBT->setText(tr("涨停"));
    connect(m_pFontBT, SIGNAL(clicked()), this, SLOT(slotChangeFont()));

    m_pColorBT = new QPushButton(m_pDisplayGB);
    m_pColorBT->setGeometry(55,70,45,25);
    m_pColorBT->setStyleSheet("background-color:blue");
    connect(m_pColorBT, SIGNAL(clicked()), this, SLOT(slotChangeColor()));



    //坐标位置
    m_pPosGB = new QGroupBox(widget);
    m_pPosGB->setGeometry(10, 140, 230, 85);
    m_pPosGB->setTitle(tr("坐标位置"));

    QLabel *labelX = new QLabel(m_pPosGB);
    labelX->setGeometry(10,30,30,18);
    labelX->setText(tr("X 坐标:"));

    QLabel *labelY = new QLabel(m_pPosGB);
    labelY->setGeometry(10,55,30,18);
    labelY->setText(tr("Y 坐标:"));

    m_pSpinBoxX = new QSpinBox(m_pPosGB);
    m_pSpinBoxX->setGeometry(50,30,50,18);
    m_pSpinBoxX->setRange(0, 1000);
    m_pSpinBoxX->setValue(800);
    connect(m_pSpinBoxX, SIGNAL(valueChanged(int)), this, SLOT(slotChangeXPosition(int)));

    m_pSpinBoxY = new QSpinBox(m_pPosGB);
    m_pSpinBoxY->setGeometry(50,55,50,18);
    m_pSpinBoxY->setRange(0, 1000);
    m_pSpinBoxY->setValue(990);
    connect(m_pSpinBoxY, SIGNAL(valueChanged(int)), this, SLOT(slotChangeYPosition(int)));


    QGridLayout *gridLayout = new QGridLayout(widget);
    gridLayout->addWidget(alwaysRB, 0, 0, 1, 1);
    gridLayout->addWidget(hideRB, 0, 1, 1, 1);
    gridLayout->addWidget(neverRB, 0, 2, 1, 1);

    gridLayout->addWidget(m_pDisplayGB, 1, 0, 1, 3);
    gridLayout->addWidget(m_pPosGB, 2, 0, 1, 3);


    return widget;
}

QWidget* StockSetting::createTabTwoInFPW()
{
    QWidget *widget = new QWidget;
    widget->setStyleSheet("background-color:yellow");

    return widget;
}

QTabWidget* StockSetting::createMDWidget()
{
    QTabWidget *firstPageWidget = new QTabWidget;
    firstPageWidget->setTabShape(QTabWidget::Triangular);

    QWidget *widget1 = createTabOneInFPW();
    QWidget *widget2 = createTabTwoInFPW();

    firstPageWidget->addTab(widget1, tr("显示"));
    firstPageWidget->addTab(widget2, tr("自选股"));

    return firstPageWidget;
}

QTabWidget* StockSetting::createSecondPageWidget()
{
    QTabWidget *secondPageWidget = new QTabWidget;
    secondPageWidget->setStyleSheet("background-color:yellow");

    return secondPageWidget;
}

QTabWidget* StockSetting::createThirdPageWidget()
{
    QTabWidget *thirdPageWidget = new QTabWidget;
    thirdPageWidget->setStyleSheet("background-color:blue");

    return thirdPageWidget;
}

void StockSetting::displayWidget()
{
    m_pStackedWidget = new QStackedWidget(this);
    m_pStackedWidget->setMinimumWidth(280);
    m_pStackedWidget->setMinimumHeight(270);

    QTabWidget *firstPageWidget = createMDWidget();
    QTabWidget *secondPageWidget = createSecondPageWidget();
    QTabWidget *thirdPageWidget = createThirdPageWidget();

    m_pStackedWidget->addWidget(firstPageWidget);
    m_pStackedWidget->addWidget(secondPageWidget);
    m_pStackedWidget->addWidget(thirdPageWidget);

    m_pStackedWidget->setCurrentIndex(0);
}

void StockSetting::initWindow()
{
    indexWidget();
    displayWidget();

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addWidget(m_pListWidget);
    layout->addWidget(m_pStackedWidget);
}

void StockSetting::slotRowChanged(int index)
{
    qDebug()<<"-------------------------------index:"<<index;
    m_pStackedWidget->setCurrentIndex(index);
}

void StockSetting::slotMicroDisplay(int id)
{
    qDebug()<<"--------------------micro is display:"<<id;

    if(id == MIC_NERVER_DIS)
    {
        m_pDisplayGB->setEnabled(false);
        m_pPosGB->setEnabled(false);

        emit signalMicroDisplayClose();
    }
    else
    {
        QFont font = m_pFontBT->font();
        QPixmap map = QPixmap::grabWidget (m_pColorBT, m_pColorBT->rect());
        QColor color(map.toImage().pixel(5,5));

        QPoint point(m_pSpinBoxX->value(), m_pSpinBoxY->value());

        if(id == MIC_HIDE_DIS)
        {
            m_pDisplayGB->setEnabled(true);
            m_pPosGB->setEnabled(true);
            emit signalMicroDisplay(id, font, color, point);

        }
        else if(id == MIC_ALWAYS_DIS)
        {
            m_pDisplayGB->setEnabled(true);
            m_pPosGB->setEnabled(true);
            emit signalMicroDisplay(id, font, color, point);
        }
    }
}

void StockSetting::slotChangeFont()
{
    qDebug()<<"--------------------slotChangeFont";

    bool ok = false;

    QFont initial = m_pFontBT->font();
    QFont font = QFontDialog::getFont (&ok, initial, 0, tr("选择字体"));
    if(ok)
    {
        m_pFontBT->setFont(font);
        emit signalChangedMDFont(font);
    }
}

void StockSetting::slotChangeColor()
{
    qDebug()<<"--------------------slotChangeColor";

    QPixmap map = QPixmap::grabWidget (m_pColorBT, m_pColorBT->rect());
    QColor initial(map.toImage().pixel(5,5));
    QColor color = QColorDialog::getColor(initial, 0, "hello",
                                          QColorDialog::ShowAlphaChannel);

    if(color.isValid())
    {
        QString str = QString::fromLatin1("background-color:%1").arg(color.name());
        m_pColorBT->setStyleSheet(str);

        emit signalChangedMDColor(color);
    }

}

void StockSetting::slotChangeXPosition(int x)
{
    QPoint point(x, m_pSpinBoxY->value());
    emit signalChangedMDPosition(point);
}

void StockSetting::slotChangeYPosition(int y)
{
    QPoint point(m_pSpinBoxX->value(), y);
    emit signalChangedMDPosition(point);
}
